$(function () {
    function sck(k, v, t) {
        let d = new Date();
        d.setTime(d.getTime() + (t));
        let expires = "expires="+ d.toUTCString();
        document.cookie = k + "=" + v + ";" + expires + ";path=/";
    }


    //Demo
    layui.use(['form','element','table'], function(){
        let table = layui.table;
        let data;
        let layer=layui.layer;
        /*异常成绩表格赋值*/
        table.render({
            elem: '#studentErrorData',
            url:'/getAbnormal',
            parseData:function(msg){
                data=msg;
                for (let i = 0; i < msg.data.length; i++) {
                    msg.data[i].time=msg.data[i].time.replace("T"," ").substr(0,19);
                    msg.data[i].major=`${msg.data[i].id}`.replace("1","广播电视编导").replace("2","戏剧影视文学").replace("3","播音与主持艺术").replace("4","航空服务艺术与管理");
                    msg.data[i].view=`<a class="view-achievement" href="javascript:void(0)">查看</a>`;
                    msg.data[i].achievementType=`${msg.data[i].type}`.replace("1","笔试").replace("2","面试");
                }
                return msg;
            },
            /*接口地址。默认会自动传递两个参数：?page=1&limit=30（该参数可通过 request 自定义）
              page 代表当前页码、limit 代表每页数据量*/
            toolbar: '#d', //开启头部工具栏，并为其绑定左侧模板
            defaultToolbar: ['exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field:'registration', width:100, title: '报名号', sort: false},
                {field:'sname', width:100, title: '学生'},
                {field:'uname', width:100, title: '登分员'},
                {field:'major', width:200, title: '专业', sort: false},
                {field:'achievementType', width:100, title: '成绩类型',sort: false},
                {field:'total', width:100, title: '成绩',sort: false},
                {field:'time', width:180, title: '时间',sort: false},
                {field:'view', width:60, title: '操作',sort: false}
            ]],
            page: true
            //count:100
            //data:data.data
        });
        /*异常成绩表格赋值*/
        table.render({
            elem: '#studentSingleData',
            url:'/findSingleAchievement',
            parseData:function(msg){
                for (let i = 0; i < msg.data.length; i++) {
                    msg.data[i].time=msg.data[i].time.replace("T"," ").substr(0,19);
                    msg.data[i].major=`${msg.data[i].id}`.replace("1","广播电视编导").replace("2","戏剧影视文学").replace("3","播音与主持艺术").replace("4","航空服务艺术与管理");
                    msg.data[i].view=`<a class="view-achievement" href="javascript:void(0)">查看</a>`;
                    msg.data[i].achievementType=`${msg.data[i].type}`.replace("1","笔试").replace("2","面试");
                }
                return msg;
            },
            /*接口地址。默认会自动传递两个参数：?page=1&limit=30（该参数可通过 request 自定义）
              page 代表当前页码、limit 代表每页数据量*/
            toolbar: '#d', //开启头部工具栏，并为其绑定左侧模板
            defaultToolbar: ['exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field:'registration', width:100, title: '报名号', sort: false},
                {field:'sname', width:100, title: '学生'},
                {field:'uname', width:100, title: '登分员'},
                {field:'major', width:200, title: '专业', sort: false},
                {field:'achievementType', width:100, title: '成绩类型',sort: false},
                {field:'total', width:100, title: '成绩',sort: false},
                {field:'time', width:180, title: '时间',sort: false},
            ]],
            page: true
            //count:100
            //data:data.data
        });
        /*学生成绩表格赋值*/
        table.render({
            elem: '#studentData',
            url:'/selectAllScore',
            parseData:function(msg){
                //data=msg;
                for (let i = 0; i < msg.data.length; i++) {
                    if (msg.data[i].type===1){
                        msg.data[i].written=msg.data[i].total;
                    }else {
                        msg.data[i].interview=msg.data[i].total;
                    }
                }
                return msg;
            },
            /*接口地址。默认会自动传递两个参数：?page=1&limit=30（该参数可通过 request 自定义）
              page 代表当前页码、limit 代表每页数据量*/
            toolbar: '#d', //开启头部工具栏，并为其绑定左侧模板
            defaultToolbar: ['filter','exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field:'registration', width:100, title: '报名号', sort: false},
                {field:'name', width:100, title: '学生姓名'},
                {field:'candidate', width:160, title: '考生号', sort: false},
                {field:'identity', width:200, title: '身份证号'},
                {field:'gender', width:80, title: '性别', sort: false},
                {field:'interview', width:100, title: '面试成绩',sort: false},
                {field:'written', width:100, title: '笔试成绩',sort: false}
                ]],
            page: true
        });
        $("html").on("click",".view-achievement",function () {
            let index=Number($(this).parent().parent().parent().attr("data-index"));
            console.log(data);
            $.ajax({
                url:`/choiceScore?aid=${data.data[index].aid}`,
                type:`get`,
                dataType:`json`,
                success:function (msg) {
                    if (msg.ok){
                        console.log("ach"+msg.achievements2);
                        sck("s",JSON.stringify({
                            "registration":msg.registration,
                            "studentName":msg.name,
                            "studentSid": msg.studentSid,
                            "subjects":msg.url.substr(1),
                            "achievements":msg.achievements,
                            "achievements2":msg.achievements2
                        }),5*1000);
                        window.location.href=msg.url;
                    }else {
                        layer.msg("error!");
                    }
                }
            });
        }).on("click","#modify-type",function () {
            //let formData=new FormData();
            let parp={
                "radio" : 0,
                "theatre":0
            };
            $("input[type=radio]:checked").each(function() {
               // console.log($(this).attr("name"),$(this).val())
                //formData.append($(this).attr("name"),$(this).val());
                parp[$(this).attr("name")]=$(this).val();
            });
        console.log(parp);
            $.ajax({
                url:`/setExamType`,
                type: "get",
                dataType: "json",
                data:`radio=${parp.radio}&theatre=${parp.theatre}`,
                success:function (msg) {
                    if (msg.ok){
                        layer.msg("修改成功");
                    }else {
                        layer.msg("修改失败");
                    }
                }
            })

        });
    });
});